<?php
namespace RMD\AccountBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use RMD\AccountBundle\Form\Type\GroupSetType;

use RMD\AccountBundle\Entity\GroupSet;


class GroupsController extends Controller
{
	public function indexAction($listid){
		$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find($listid);
		if (!$list || !$list->isOwnedBy($this->getUser())) {
			throw $this->createNotFoundException();
		}
		$groupSets = $list->getGroupSets();
		return $this->render('RMDAccountBundle:Groups:index.html.php', array('groupSets' => $groupSets, 'listid' => $listid));
	}
	public function addAction($listid){
		$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find($listid);
		if (!$list || !$list->isOwnedBy($this->getUser())) {
			throw $this->createNotFoundException();
		}
		$groupSet = new GroupSet();		
		$form = $this->createForm(new GroupSetType(), $groupSet);
		$request = $this->getRequest();		
		if ($request->isMethod('POST')) {
			$form->bind($request);
			if ($form->isValid()) {
				$groupSet->setList($list);
				$em = $this->getDoctrine()->getManager();
				$em->persist($groupSet);
				// cascade persist in GropSet.orm.yml
				/*foreach ($groupSet->getGroups() as $group) {
					$em->persist($group->setGroupSet($groupSet));
				}*/
				$em->flush();
				$session = $this->get('session');
				$session->setFlash('success', 'Group created');
				return $this->redirect($this->generateUrl('account_groups', array('listid' => $listid)));
			}
		}
		return $this->render('RMDAccountBundle:Groups:add.html.php',
		array('form' => $form->createView(), 'listid' => $listid));
	}
	public function deleteAction($id){
		$groupSet = $this->getDoctrine()->getRepository('RMDAccountBundle:GroupSet')->find($id);
		$session = $this->get('session');
		if (!$groupSet || !$list = $groupSet->getList() || !$list->isOwnedBy($this->getUser())) {
			$session->setFlash('error', 'Given group not found');				
			return $this->redirect($this->generateUrl('account_groups', array('listid' => $list->getId())));
		}
		$listid = $groupSet->getList()->getId();
		$em = $this->getDoctrine()->getManager();
		// cascade remove in GropSet.orm.yml
		$em->remove($groupSet);
		$em->flush();
		$session->setFlash('success', 'Group deleted');
		return $this->redirect($this->generateUrl('account_groups', array('listid' => $listid)));
	}
	public function editAction($id){
		$groupSet = $this->getDoctrine()->getRepository('RMDAccountBundle:GroupSet')->find($id);
		if (!$groupSet || !$list = $groupSet->getList() || !$list->isOwnedBy($this->getUser())) {
			throw $this->createNotFoundException();
		}
		$listid = $groupSet->getList()->getId();
		$originalGroups = array();
		foreach ($groupSet->getGroups() as $group) {
			$originalGroups[] = $group;
		}
		$form = $this->createForm(new GroupSetType(), $groupSet);
		$form->remove('selectType');
		$selectType = $groupSet->getSelectTypeText();
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$form->bind($request);
			if ($form->isValid()) {
				$session = $this->get('session');
				//calculating which groups need to be removed
				foreach ($originalGroups as $key => $origGroup) {				
					foreach ($groupSet->getGroups() as $group) {
						if ($origGroup->getId() === $group->getId()) {
							unset($originalGroups[$key]);
						}
					}
				}
				$em = $this->getDoctrine()->getManager();
				foreach ($originalGroups as $origGroup) {
					$em->remove($origGroup);
				}
				// cascade persist in GropSet.orm.yml
				/*foreach ($groupSet->getGroups() as $group) {
					$em->persist($group);
				}*/
				$em->persist($groupSet);
				$em->flush();
				$session->setFlash('success', 'Group modified');
				return $this->redirect($this->generateUrl('account_groups', array('listid' => $listid, 'selectType' => $selectType)));
			}
		}
		return $this->render('RMDAccountBundle:Groups:edit.html.php',
				array('form' => $form->createView(), 'id' => $id, 'listid' => $listid, 'selectType' => $selectType));
	}
	public function countSubscribersAction($groupId){
		$res = $this->getDoctrine()->getRepository('RMDAccountBundle:Group')->countSubscribers($groupId);
		return new Response($res);
	}
}
